import json

class player(object):
    def __init__(self, name):
        self.name = name
        self.life = 10
        self.power = 1
        self.x = 0
        self.y = 0
    
    def to_map(self):
        return {"name":self.name, 
            "properties":{
                "life":self.life, 
                "power":self.power}
            }
    
    def serialize(self):
        return json.write(self.to_map())

class action(object):
    def __init__(self, type, value):
        """
        type     meaning
        0        attack
            value    meaning
            0        left 1
            1        up 1
            2        right 1
            3        down 1
        1        move
            value    meaning
            0        left 1
            1        up 1
            2        right 1
            3        down 1
        """
        self.type = type
        self.value = value

    def to_map(self):
        return {"type":self.type, "value":self.value}

    def serialize(self):
        return json.write(self.to_map())

def deserialize_player(str):
    map = json.read(str)
    p = player(map["name"])
    p.life = map["properties"]["life"]
    p.power = map["properties"]["power"]
    return p

def deserialize_action(str):
    map = json.read(str)
    return action(map["type"], map["value"])
    

